<?php

	session_start();

	if (!isset($_SESSION['id_user']))
		$_SESSION['id_user'] = 0;
	if (!isset($_SESSION['user_info']))
		$_SESSION['user_info'] = array();
	if (!isset($_SESSION['sid_value']))
		$_SESSION['sid_value'] = '';
	if (!isset($_SESSION['sid_expire']))
		$_SESSION['sid_expire'] = time()+24*60*60;

	if (!empty($_GET['logout']))
	{
		$_SESSION['id_user'] = 0;
		$_SESSION['user_info'] = array();
		$_SESSION['sid_value'] = '';
	}

	if (!empty($_GET['sid']))
	{
		$_SESSION['id_user'] = 0;
		$_SESSION['sid_value'] = $_GET['sid'];
		$_SESSION['sid_expire'] = time()+24*60*60;
		$_SESSION['user_info'] = array();
	}
	if ($_SESSION['id_user'] && $_SESSION['sid_value'] && ($_SESSION['sid_expire'] < time()))
	{
		$_SESSION['id_user'] = 0;
		$_SESSION['user_info'] = array();
	}

	include_once 'mgates-class.php';

	if (!$_SESSION['id_user'] && $_SESSION['sid_value'])
	{
		$res = $mgates->getUserInfo($_SESSION['sid_value']);
		$_SESSION['sid_value'] = "";
		if ($res)
		{
			$_SESSION['id_user'] = $res['id'];
			$_SESSION['sid_value'] = $res['sid'];
			$_SESSION['sid_expire'] = time()+24*60*60;
			$_SESSION['user_info'] = $res;
			$_SESSION['mgates_info'] = $mgates->getMiscInfo($_SESSION['sid_value']);
		}
	}

	if (empty($_SESSION['mgates_info']))
		$_SESSION['mgates_info'] = $mgates->getMiscInfo();
	
?>

<html>
<body>
<?php
	if ($_SESSION['id_user'])
	{ 
?>
	<div>
		<img src="<?php echo $_SESSION['user_info']['avatar']; ?>" /><br/>
		SID: <?php echo $_SESSION['sid_value']; ?><br/>
		ID: <?php echo $_SESSION['id_user']; ?><br/>
		Subst: <?php echo $_SESSION['user_info']['substitutor']; ?><br/>
		Login: <?php echo $_SESSION['user_info']['login']; ?><br/>
		Permanent: <?php echo $_SESSION['user_info']['permanent']; ?><br/>
		Phone: <?php echo $_SESSION['user_info']['phone']; ?><br/>
		Sex: <?php echo $_SESSION['user_info']['sex']; ?><br/>
		Birthdate: <?php echo $_SESSION['user_info']['birthdate']; ?><br/>
		Own user: <?php echo $_SESSION['user_info']['own']; ?><br/>
		Place code: <?php echo $_SESSION['user_info']['placecode']; ?><br/>
		Balance: <?php echo $_SESSION['user_info']['balance']; ?><br/>
		Bonus: <?php echo $_SESSION['user_info']['bonus']; ?><br/>
		Paid: <?php echo $_SESSION['user_info']['paid']; ?><br/>
		Status: <?php echo $_SESSION['user_info']['status']; ?><br/>
		Mobile: <?php echo $_SESSION['user_info']['mobile']; ?><br/>
		Mobile X: <?php echo $_SESSION['user_info']['mobile_x']; ?><br/>
		Mobile Y: <?php echo $_SESSION['user_info']['mobile_y']; ?><br/>
	</div><br/>
<?php
	}
?>
	<div>
		<?php if (!$_SESSION['id_user']) echo '<a href="?sid=1">Enter</a>'; else echo '<a href="?logout=1">Exit</a>'; ?>
		<?php if (!empty($_SESSION['mgates_info']['url_main'])) echo '<br/><a href="'.$_SESSION['mgates_info']['url_main'].'">mGates</a>'; ?>
	</div>
</body>
</html>
